package org.index.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.index.colum.Ad;
import org.index.entity.AdDataMapping;
import org.index.service.AdSource;
import org.operator.jdbc.D;
import org.shop.common.dao.base.IBaseDao;
import org.shop.common.service.base.impl.BaseServiceImpl;
import org.shop.common.until.AbstractPageResult;
import org.shop.common.until.AjaxPage;
import org.shop.common.until.PageResultList;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import redis.clients.jedis.JedisShardInfo;

/**
 * 返回广告
 * @author Administrator
 */
public class ResponseAdServiceImpl extends BaseServiceImpl<AdDataMapping> implements AdSource<AdDataMapping>{
	
	/**
	 * @param param
	 * @param splitKey
	 * @param objEntityClass
	 */
	public ResponseAdServiceImpl(Map<String, String> param, String splitKey, AdDataMapping objEntityClass) {
		super(param, splitKey, objEntityClass);
		// TODO Auto-generated constructor stub
	}
	
	public ResponseAdServiceImpl(Map<String, String> param, String splitKey) {
		
		super(param, splitKey, new AdDataMapping());
	}
	
	public ResponseAdServiceImpl()
	{
		super(new HashMap<>(), "", new AdDataMapping());
	}
	
	/**
	 * 获取广告列表
	 * @return 
	 */
	public List<AdDataMapping> getAdList() {
		// TODO Auto-generated method stub

		RedisTemplate<String, List<AdDataMapping>> redisTemplate = new RedisTemplate<String, List<AdDataMapping>>();
		
		redisTemplate.setConnectionFactory(new JedisConnectionFactory(new JedisShardInfo("localhost", 6379, "ggh")));
		
		redisTemplate.afterPropertiesSet();
		
		ValueOperations<String, List<AdDataMapping>> operation = redisTemplate.opsForValue(); 
		
		String key = "ad_catch_what";
		
		List<AdDataMapping> data = operation.get(key);
		
		if (data != null) {
			return data;
		}
		
		String field = this.getSelectField();
		
		Ad objCoulm = (Ad)this.getobjColum();
		
		String where = objCoulm.getAdSpaceId() + " = ? and "+ objCoulm.getEnabled() + "=?";
		
		data = D.M(AdDataMapping.class).field(field.toString()).where(where, 50, 1).select();
		
		if (data == null) {
			return new ArrayList<>();
		}
		
		operation.set(key, data, 300, TimeUnit.SECONDS);
		
		return data;
	}

	/**
	 * 获取查询字段
	 * @return
	 */
	protected String getSelectField()
	{
		Ad objCoulm = (Ad)this.getobjColum();
		
		StringBuffer field = new StringBuffer(objCoulm.getId());

		field.append(",").append(objCoulm.getAdLink());
		
		field.append(",").append(objCoulm.getPicUrl());
		
		field.append(",").append(objCoulm.getStartTime());
		
		field.append(",").append(objCoulm.getEndTime());
		System.out.println(field);
		return field.toString();
	}
	
	@Override
	protected IBaseDao<AdDataMapping> getMapper() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String geColumClassName() {
		// TODO Auto-generated method stub
		return "Ad";
	}

	protected Class<?> geObjEntityClass() {
		// TODO Auto-generated method stub
		return AdDataMapping.class;
	}


	public AbstractPageResult<AdDataMapping> getBanner() {
		// TODO Auto-generated method stub
//		RedisTemplate<String, AbstractPageResult<AdDataMapping>> redisTemplate = new RedisTemplate<String, AbstractPageResult<AdDataMapping>>();
//		
//		redisTemplate.setConnectionFactory(new JedisConnectionFactory(new JedisShardInfo("localhost", 6379, "ggh")));
//		
//		redisTemplate.afterPropertiesSet();
//		
//		ValueOperations<String, AbstractPageResult<AdDataMapping>> operation = redisTemplate.opsForValue(); 
		
		String key = "ad_catch_banner";
		
		AbstractPageResult<AdDataMapping> mapData = null ;//= operation.get(key);
		
		mapData = this.getDataList();
		
		if (mapData == null) {
			return mapData;
		}
		
//		operation.set(key, mapData, 300, TimeUnit.SECONDS);
		
		return mapData;
	}
	
	/**
     * 获取搜索字段
     * @return
     */
    protected String [] searchArray() {
    	
    	Ad objCoulm = (Ad)this.getobjColum();
    	
    	return new String[]{
    		objCoulm.getType()
    	};
    }
    
    /**
     * 
     * @param dataList
     * @param pageObj
     * @return
     */
    protected  AbstractPageResult<AdDataMapping> getResult(List<AdDataMapping> dataList, AjaxPage pageObj) {
    	
    	PageResultList<AdDataMapping> pageResult = new PageResultList<AdDataMapping>();
    	
    	pageResult.setPageNo(pageObj.getNowPage());
    	
    	pageResult.setPages(pageObj.getTotalPages());
    	
    	pageResult.setPageSize(pageObj.getListRows());
    	
    	pageResult.setTotal(pageObj.getTotalRows());
    	
    	pageResult.setDataList(dataList);
    	
        return pageResult;
    }
    
	
}
